package com.sscaipiao.android.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.sscaipiao.android.R;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.utils.UserDataServiceHelper;

public class ResetPasswordActivity extends Activity implements OnClickListener {

	private Button backButton;
	private Button finishButton;

	
	private EditText  old_password;
	private EditText  new_password;
	private EditText  new_repeat_password;
	
	
	private User user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork()
		.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
		.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
		.penaltyLog().penaltyDeath().build());

		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_password);
		
		Intent i = this.getIntent();
		if (!i.equals(null)) {
			Bundle b = i.getExtras();
			if (b != null) {
				if (b.containsKey(Constants.USER)) {
					user = (User) b.get(Constants.USER);
				}
			}
		}
		
		backButton =  (Button) findViewById(R.id.reset_password_button_back);
		finishButton = (Button) findViewById(R.id.reset_password_button_finish);
		
		backButton.setOnClickListener(this);
		finishButton.setOnClickListener(this);
		
		old_password = (EditText)  findViewById(R.id.reset_password_old_pass);
		new_password = (EditText)  findViewById(R.id.reset_password_txt_password);
		new_repeat_password = (EditText)  findViewById(R.id.reset_password_txt_repeat_password);
		
	}

	
	@Override
	public void onClick(View v) {
		if(R.id.reset_password_button_back == v.getId()) {

			ResetPasswordActivity.this.finish();  
		} else if (R.id.reset_password_button_finish == v.getId()) {
			
			String errorMsg = "";
			
			if(old_password.getText() == null || StringUtil.isEmptyString(old_password.getText().toString()) || 
					!old_password.getText().toString().equals( user.getPassword())){
				errorMsg += "旧密码不正确\n";
				
			}
			if(new_password.getText() == null || new_repeat_password.getText() == null || StringUtil.isEmptyString(new_password.getText().toString()) || 
					StringUtil.isEmptyString(new_repeat_password.getText().toString())){
				errorMsg += "新密码不能为空\n";
				
			} else if( !new_password.getText().toString().equals(new_repeat_password.getText().toString()) ){
				errorMsg += "2次输入新密码不匹配\n";
				
			}
			if( new_password.getText().length() < 6 || new_password.getText().length() >15){
				errorMsg += "新密码长度为6-15位\n";
			} 

			 if( StringUtil.isNotEmptyString(errorMsg)) {
					new AlertDialog.Builder(this).setTitle("输入数据不完整")  
	                .setMessage(errorMsg)  
	                .setPositiveButton("确定", null)  
	                .show(); 
			 }else {
				 UserDataServiceHelper.saveNewPassword(ResetPasswordActivity.this, user, new_repeat_password.getText().toString());
					Intent intent = new Intent(ResetPasswordActivity.this, Regist2Activity.class);
					Bundle b = new Bundle();
					if (user == null) {
						user = new User();
					}
					
					b.putSerializable(Constants.USER, user);
					intent.putExtras(b);
					startActivity(intent);

			 }
			 
		} else {
			return;
		}
	}

	

}
